<template>
  <div id="searchQuestionSubmitView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="questionId" label="题号">
        <a-input
          v-model="searchParams.questionId"
          placeholder="请输入搜索题号"
          :style="{ width: '150px', marginRight: '16px' }"
        />
        <a-select
          v-model="searchParams.language"
          :style="{ width: '160px' }"
          placeholder="选择编程语言"
          :trigger-props="{ autoFitPopupMinWidth: true }"
        >
          <a-option disabled="true" :style="{ color: 'blue' }"
            >选择语言
          </a-option>
          <a-option>java</a-option>
          <a-option>cpp</a-option>
          <a-option>go</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button @click="doSearch" type="primary">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #id="{ record }">
        {{ record.id.substring(record.id.length - 5) }}
      </template>
      <template #language="{ record }">
        {{ record.language }}
      </template>
      <template #message="{ record }">
        {{ record.judgeInfo.message }}
      </template>
      <template #memory="{ record }">
        {{ (record.judgeInfo.memory / (1024 * 1024)).toFixed(2) }}
        <span v-if="record.judgeInfo.memory">MB</span>
      </template>
      <template #time="{ record }">
        {{ (record.judgeInfo.time / 1000).toFixed(2)
        }}<span v-if="record.judgeInfo.time">S</span></template
      >
      <template #status="{ record }">
        {{
          record.status == 1
            ? "判题中"
            : record.status == 2
            ? "判题成功"
            : record.status == 3
            ? "判题失败"
            : "待判题"
        }}
      </template>
      <template #questionId="{ record }">
        {{ record.questionId.substring(record.questionId.length - 5) }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
      <!--      <template #optional="{ record }">
              <a-space>
                <a-button type="primary" @click="doQuestion(record)">做题</a-button>
              </a-space>
            </template>-->
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionSubmitQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute, useRouter } from "vue-router";
import moment from "moment";
import * as path from "path";

const router = useRouter();
const route = useRoute();
const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  questionId: undefined,
  language: "",
  pageSize: 10,
  current: 1,
} as QuestionSubmitQueryRequest);

const onPageChange = (pageNumber: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: pageNumber,
  };
};
const loadData = async () => {
  const result =
    await QuestionControllerService.listQuestionSubmitByPageUsingPost({
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    });
  if (result.code === 0) {
    dataList.value = result.data.records;
    total.value = result.data.total;
  } else {
    message.error("加载失败，" + result.message);
    await router.push({
      path: "/user/login",
    });
  }
};

/**
 * watchEffect的作用：监听传入的方法里的所有的变量，一旦有一个值发生改变就会触发loadData()方法
 * 这里是监听 searchParams 变量，变量改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});
/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "提交号",
    slotName: "id",
  },
  {
    title: "编程语言",
    slotName: "language",
  },
  {
    title: "判题结果",
    slotName: "message",
  },
  {
    title: "占用内存",
    slotName: "memory",
  },
  {
    title: "执行时间",
    slotName: "time",
  },
  {
    title: "判题状态",
    slotName: "status",
  },
  {
    title: "题号",
    slotName: "questionId",
  },
  {
    title: "提交者",
    dataIndex: "userVO.userAccount",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
];

/**
 * 点击搜索触发事件
 */
const doSearch = () => {
  //为了防止当前页不为1时，搜索不到数据，每次执行该事件时，都重新给当前页赋值
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  // 重新根据搜索参数，加载数据
  //这里可以不用执行loadData方法，因为上面的 watchEffect已经监听了searchParams，searchParams发生改变就会执行loadData方法
  // loadData();
};
</script>

<style scoped>
#searchQuestionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
